#include <stdio.h>
#include <jni.h>
#include <stdlib.h>

// C++灰化图片的方法
jintArray nativeGrayPic(JNIEnv *env, jclass clazz, jintArray buf, jint w, jint h) {
    jint *cbuf;
    cbuf = env->GetIntArrayElements(buf, JNI_FALSE);
    if (cbuf == NULL) {
        return 0; /* exception occurred */
    }
    jint alpha = 0xFF << 24;
    for (jint i = 0; i < h; i++) {
        for (jint j = 0; j < w; j++) {
            // 获得像素的颜色
            jint color = cbuf[w * i + j];
            jint red = ((color & 0x00FF0000) >> 16);
            jint green = ((color & 0x0000FF00) >> 8);
            jint blue = color & 0x000000FF;
            color = (red + green + blue) / 3;
            color = alpha | (color << 16) | (color << 8) | color;
            cbuf[w * i + j] = color;
        }
    }
    jint size=w * h;
    jintArray result = env->NewIntArray(size);
    env->SetIntArrayRegion(result, 0, size, cbuf);
    env->ReleaseIntArrayElements(buf, cbuf, 0);
    return result;
}

/**
 * JNINativeMethod由三部分组成:
 * (1)Java中的函数名;
 * (2)函数签名,格式为(输入参数类型)返回值类型;
 *  ([III)[I ([III)表示三个参数，依次为int[]、int、int；[I 表示返回值类型为int[]
 * (3)native函数名
 */
static JNINativeMethod gMethods[] = {
        {"grayPic", "([III)[I", (void *) nativeGrayPic }
};

//System.loadLibrary过程中会自动调用JNI_OnLoad，在此进行动态注册
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
    JNIEnv *env = NULL;
    jint result = JNI_FALSE;

    //获取env指针
    if (jvm->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_OK) {
        return result;
    }
    if (env == NULL) {
        return result;
    }
    //获取类引用
    jclass clazz = env->FindClass("com/example/testndk/JNIDynamicUtils");
    if (clazz == NULL) {
        return result;
    }
    //注册方法
    if (env->RegisterNatives(clazz, gMethods, sizeof(gMethods) / sizeof(gMethods[0])) < 0) {
        return result;
    }
    //成功
    result = JNI_VERSION_1_6;
    return result;
}